<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增会员卡分类 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <Drawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="vipcardCategory">
import { defineComponent } from 'vue';
import { BasicTable, useTable, TableAction } from '@apps/antd-admin/src/components/Table';
import { page, remove } from './api';
import { useDrawer } from '@apps/antd-admin/src/components/drawer';
import Drawer from './drawer.vue';
import { columns, searchFormSchema } from './data';
import { useMessage } from '@apps/antd-admin/src/hooks/web/useMessage';

const { createMessage } = useMessage();
const [registerDrawer, { openDrawer }] = useDrawer();
const [registerTable, { reload }] = useTable({
  title: '会员卡分类列表',
  api: page,
  columns,
  formConfig: {
    labelWidth: 120,
    schemas: searchFormSchema,
  },
  useSearchForm: true,
  showTableSetting: true,
  bordered: true,
  showIndexColumn: true,
  actionColumn: {
    width: 80,
    title: '操作',
    dataIndex: 'action',
    slots: { customRender: 'action' },
    fixed: 'right',
  },
});

function handleCreate() {
  openDrawer(true, {
    isUpdate: false,
  });
}

function handleEdit(record: Recordable) {
  openDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleDelete(record: Recordable) {
  console.log(record);
  const id = record.vipcardCategoryId;
  remove(id)
    .then((data) => {
      createMessage.success('删除成功');
      handleSuccess();
    })
    .catch((data) => {
      createMessage.error('删除失败');
    });
}

function handleSuccess() {
  reload();
}
</script>
